local parser = require 'types.record.parser'
local mapper = require 'types.record.mapper'

local chars = { }
local bytes = { }
for i = 1, 12 do 
    chars[#chars+1] = ":i4"
    bytes[#bytes+1] = 0
end 

local define = parser.new(chars, bytes)

local mt = {}
mt.__index = mt 

function mt:get(index)
    assert( index<=12, "索引超过上限" )
    return self[index]
end

function mt:set(index, value)
    assert( index<=12, "索引超过上限" )
    if value > 2147483647 then 
        value = 2147483647
    elseif value < -2147483648 then 
        value = -2147483648
    end 
    self[index] = value
end

-- 读取服务器保存的整数值
function ac.player.__index :get_stored_integer(name)
	if not self.integer_stores then
		return 0 
	end
    local count = mapper.index("integer", name)
    if not count then 
        log.error("没有声明的就进行使用的“integer”类型存档", name)
        return false 
    end 
    local lcount = math.ceil(count / 12)
	local lindex = count % 12
    local lstore = self.integer_stores[lcount]
	if lindex == 0 then 
		lindex = 12
	end 
	return lstore:get(lindex)
end 

-- 保存布尔值到服务器上
function ac.player.__index:store_integer(name, value)
	if not self.integer_stores then
		return false 
	end
    local count = mapper.index("integer", name)
    if not count then 
        log.error("没有声明的就进行使用的“integer”类型存档", name)
        return false 
    end 
    local lcount = math.ceil(count / 12)
	local lindex = count % 12
	if lindex == 0 then 
		lindex = 12
	end 
    local lname = ("integer:%d"):format(lcount)
    local index = mapper.index('archive', lname)
    local sname = ("arc%d"):format(index)
    local lstore = self.integer_stores[lcount]
	lstore:set(lindex, value)
	self:save_server_safe(sname, define:encode(lstore))
    local cindex = mapper.index('isscore', name)
    if cindex then 
        self:set_score(("sc%d"):format(cindex), value)
    end 
    return true
end 

function ac.player.__index:add_stored_integer(name, value)
    self:store_integer(name, self:get_stored_integer(name) + value )
end 

-- 读取服务器保存的整数值 自动转索引
function ac.player.__index :get_stored_integer_array(name, index)
    return self:get_stored_integer(("%s%d"):format(name, index))
end 

-- 保存布尔值到服务器上 自动转索引
function ac.player.__index:store_integer_array(name, index, value)
    return self:store_integer(("%s%d"):format(name, index), value)
end 

local export = { }

-- 定义一个整数类型的存档
function export.globals(name)
    mapper.define('integer', name)
end 

function export.initialize()
    local amount = math.ceil((mapper.amount "integer") / 12)
    for i = 1, 12 do 
        local player = ac.player(i)
        if player:is_online() then 
            player.integer_stores = { }
            for j = 1, amount do 
                local lname = ("integer:%d"):format(j)
                local index = mapper.define('archive', lname)
                local sname = ("arc%d"):format(index)
                player.integer_stores[j] = setmetatable(define:decode( player:load_server(sname) ), mt)
            end 
        end 
    end 
end 

return export